﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.IO;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Helpers;
using System.Web.Routing;
using Microsoft.Practices.Unity;
using Vit.Common;
using Vit.Common.Extensions;
using Vit.Common.Providers;
using Vit.Web.Models;
using Vit.Web.Providers;
using Vit.Web.Mvc.DataAnnotations;
using Vit.Web.Helpers;

namespace Vit.Web.Extensions
{
    public static class HtmlHelperExtensions
    {
        public static IHtmlString CachePartial(this HtmlHelper html, string key, int cacheSeconds, string partialName, object model = null, ViewDataDictionary viewData = null)
        {
            ICacheProvider cache = Unity.Current.Resolve<ICacheProvider>();
            if (cacheSeconds > 0 && !string.IsNullOrWhiteSpace(key) && cache != null)
            {
                if (cache.Contains(key))
                    return cache.GetData(key) as IHtmlString;

                var result = html.Partial(partialName, model, viewData);
                cache.Add(key, result);
                return result;
            }
            return html.Partial(partialName, model, viewData);
        }

        public static IHtmlString CacheAction(this HtmlHelper html, string key, int cacheSeconds, string action, string controller = null, object routes = null)
        {
            ICacheProvider cache = Unity.Current.Resolve<ICacheProvider>();
            if (cacheSeconds > 0 && !string.IsNullOrWhiteSpace(key) && cache != null)
            {
                if (cache.Contains(key))
                    return cache.GetData(key) as IHtmlString;

                var result = html.Action(action, controller, routes);
                cache.Add(key, result);
                return result;
            }
            return html.Action(action, controller, routes);
        }

        //public static IHtmlString Partial(this HtmlHelper html, string partialName, Func<dynamic, IHtmlString> wrapper, object model = null, ViewDataDictionary viewData = null)
        //{
        //    return wrapper(html.Partial(partialName, model, viewData));
        //}

        public static HtmlHelper SetModel(this HtmlHelper html, object model)
        {
            ViewDataDictionary viewData = new ViewDataDictionary();
            foreach (var kvp in html.ViewData)
                viewData.Add(kvp.Key, kvp.Value);

            viewData.Model = model;
            HtmlHelper helper = new HtmlHelper(html.ViewContext, new Fakes.FakeViewDataContainer(viewData));
            return helper;
        }

        public static Type ModelType(this HtmlHelper html)
        {
            Type modelType = html.ViewData.Model.GetType() as Type;
            if (modelType.IsGenericType)
            {
                modelType = modelType.GetGenericArguments()[0];
            }
            return modelType;
        }
    }
}
